<template>
	<view class="page" :style="{'min-height':h+'px','padding-top':mt+'px'}">
		<c-nav-bar title="常用发票抬头"></c-nav-bar>
		<view class="list" v-if="list.length>0">
			<view v-for="(item,index) in list" :key="index" @tap.prevent="selectItem(item)">
				<view class="type">{{item.titleType==1?'个人/非企业单位':'企业单位'}}</view>
				<view class="name">
					<view class="text">
						{{item.invoiceTitle}}
						<span v-if="item.titleType==1&&item.isDefault">【默认】</span>
					</view>
					<view class="cz">
						<image src="https://i.ringzle.com/file/20231226/253aff21a5da4dbaab7630d5b0f59219.png" @tap.stop="editFptt(item)"></image>
						<image src="https://i.ringzle.com/file/20231226/e0c77b64e0d94e9e8a13c5220c40264c.png" @tap.stop="delFptt(item)"></image>
					</view>
				</view>
				<view class="code" v-if="item.titleType==0">
					<view class="text">{{item.taxNumber}}</view>
					<view class="mr" v-if="item.isDefault">【默认】</view>
				</view>
			</view>
		</view>
		<block v-else>
			<NoData/>
		</block>
		<view class="bottom">
			<view class="btn" @tap="addInvoiceTt">新增发票抬头</view>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				h: uni.getSystemInfoSync().windowHeight,
				mt: uni.getSystemInfoSync().statusBarHeight + 44,
				page:1,
				limit:10,
				finished:false,
				list:[],
				type:''
			}
		},
		onLoad(option) {
			if(option.type) this.type = option.type;
		},
		onShow() {
			this.init();
		},
		onReachBottom() {
			if(this.finished) return;
			this.getList();
		},
		methods: {
			init(){
				this.page = 1;
				this.finished = false;
				this.list = [];
				this.getList();
			},
			getList(){
				this.$api.get('/api/isinvoiceheader/page',{
					page:this.page,
					limit:this.limit,
					openId:uni.getStorageSync('userInfo')?JSON.parse(uni.getStorageSync('userInfo')).openId:''
				}).then(res=>{
					if(res.data.code===0){
						if(res.data.data.list.length==0) return this.finished = true;
						else{
							this.page++;
							this.list = [...this.list,...res.data.data.list];
						}
						if(this.type){
							this.list = this.list.filter(l=>l.titleType==(+this.type-1))
						}
					}else this.$showModal(res.data.msg);
				})
			},
			addInvoiceTt(){
				uni.navigateTo({
					url:'/pagesMy/invoice/lookupOperate?type=add'
				})
			},
			editFptt(item){
				uni.navigateTo({
					url:'/pagesMy/invoice/lookupOperate?type=edit&item='+encodeURIComponent(JSON.stringify(item))
				})
			},
			delFptt(item){
				this.$api.del('/api/isinvoiceheader',[item.id]).then(res=>{
					if(res.data.code===0){
						this.$showToast('删除成功');
						setTimeout(()=>{
							this.init();
						},1500)
					}else this.$showModal(res.data.msg);
				})
			},
			selectItem(item){
				if(this.type){
					this.getOpenerEventChannel().emit('selectCallBack', item);
					uni.navigateBack();
				}
			}
		}
	}
</script>

<style scoped lang="less">
	.page{
		background: #F5F8FA;
		padding-bottom: 184rpx;
		box-sizing: border-box;
		.list{
			&>view{
				width: 100%;
				padding: 27rpx 40rpx 33rpx 30rpx ;
				box-sizing: border-box;
				margin-top: 20rpx;
				background: #fff;
				
				.type{
					font-family: PingFangSC, PingFang SC;
					font-weight: 400;
					font-size: 28rpx;
					color: #88919D;
					line-height: 40rpx;
				}
				.name{
					display: flex;
					justify-content: space-between;
					margin-top: 25rpx;
					.text{
						width: calc(100% - 112rpx);
						padding-right: 40rpx;
						box-sizing: border-box;
						font-family: PingFangSC, PingFang SC;
						font-weight: 400;
						font-size: 30rpx;
						color: #333333;
						line-height: 42rpx;
						display: flex;
						align-items: center;
						span{
							font-family: PingFangSC, PingFang SC;
							font-weight: 400;
							font-size: 30rpx;
							color: #FEA400;
							line-height: 42rpx;
							margin-left: 20rpx;
						}
					}
					.cz{
						width: 112rpx;
						display: flex;
						align-items: center;
						justify-content: space-between;
						image{
							width: 36rpx;
							height: 36rpx;
						}
					}
				}
				.code{
					display: flex;
					align-items: center;
					margin-top: 24rpx;
					.text{
						font-family: PingFangSC, PingFang SC;
						font-weight: 400;
						font-size: 30rpx;
						color: #333333;
						line-height: 42rpx;
					}
					.mr{
						font-family: PingFangSC, PingFang SC;
						font-weight: 400;
						font-size: 30rpx;
						color: #FEA400;
						line-height: 42rpx;
						margin-left: 20rpx;
					}
				}
			}
		}
		
		.empty{
			width: 100%;
			height: 100%;
			display: flex;
			align-items: center;
			justify-content: center;
			text{
				font-size: 30rpx;
				font-family: PingFangSC, PingFang SC;
				font-weight: 400;
				color: #808080;
			}
		}
		
		.bottom{
			width: 100%;
			padding: 20rpx 24rpx 0;
			box-sizing: border-box;
			height: 164rpx;
			background: #FFFFFF;
			position: fixed;
			bottom: 0;
			left: 0;
			z-index: 2;
			.btn{
				width: 100%;
				height: 82rpx;
				background: #007A69;
				border-radius: 41rpx;
				line-height: 82rpx;
				text-align: center;
				font-size: 32rpx;
				font-family: PingFangSC, PingFang SC;
				font-weight: 400;
				color: #FFFFFF;
				letter-spacing: 2rpx;
			}
		}
	}
</style>